﻿using System;
using SaleManagement.Web.App_Code;
using SaleManagement.Domains;
using SaleManagement.Services;
using DevExpress.Web.ASPxGridView;
using System.Web.UI.HtmlControls;
using System.Data;

namespace SaleManagement.Web.Pages
{
  public partial class PaymentList : BasePage
  {
    #region Properties
    private const string SEARCH_CUSTOMER_CODE = "PaymentList_Search_Customer_Code";
    private const string SEARCH_CUSTOMER_NAME = "PaymentList_Search_Customer_Name";
    private const string SEARCH_CUSTOMER_PHONE = "PaymentList_Search_Phone";

    private string m_InnerError = string.Empty;
    private string m_InnerProperties = string.Empty;
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsCallback && !IsPostBack)
      {
        #region Load Session

        txtSearchCode.Text = GetSessionSearch(SEARCH_CUSTOMER_CODE);
        txtSearchName.Text = GetSessionSearch(SEARCH_CUSTOMER_NAME);
        txtSearchPhone.Text = GetSessionSearch(SEARCH_CUSTOMER_PHONE);

        #endregion
      }
    }

    protected void gvPayment_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
      string para = e.Parameters;
      if (para == "Do")
      {
        int customerId = 0;
        int.TryParse(hfCustomerId.Value, out customerId);
        m_InnerProperties = "Do";
        m_InnerError = DoPayment(customerId);
      }
      else if (para == "Delete")
      {
        m_InnerProperties = "Delete";
        m_InnerError = DeletePayment();
      }

      LoadPayment();
    }

    protected void gvPayment_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = m_InnerError;
      e.Properties["cpInnerProperty"] = m_InnerProperties;
    }

    protected void gvPayment_PageIndexChanged(object sender, EventArgs e)
    {
      LoadPayment();
    }
    #endregion

    #region Methods
    private void LoadPayment()
    {
      string customerCode = null;
      string customerName = null;
      string phone = null;
      if (!string.IsNullOrEmpty(txtSearchCode.Text.Trim()))
      {
        customerCode = txtSearchCode.Text.Trim();
      }

      if (!string.IsNullOrEmpty(txtSearchName.Text.Trim()))
      {
        customerName = txtSearchName.Text.Trim();
      }

      if (!string.IsNullOrEmpty(txtSearchPhone.Text.Trim()))
      {
        phone = txtSearchPhone.Text.Trim();
      }

      ConfigSessionSearch(SEARCH_CUSTOMER_CODE, customerCode);
      ConfigSessionSearch(SEARCH_CUSTOMER_NAME, customerName);
      ConfigSessionSearch(SEARCH_CUSTOMER_PHONE, phone);

      gvPayment.DataSource = new PaymentService().Search(customerCode, customerName, phone);
      gvPayment.DataBind();
      ApplyPaymentMasterLayout();
    }

    private void ApplyPaymentMasterLayout()
    {
      gvPayment.BeginUpdate();
      try
      {
        gvPayment.ClearSort();
        gvPayment.GroupBy((DevExpress.Web.ASPxGridView.GridViewColumn)gvPayment.Columns["CustomerId"]);
      }
      catch (Exception ex)
      {
        WriteLog("Payment.aspx::Apply Master Layout", ex);
      }
      finally
      {
        gvPayment.EndUpdate();
      }

      gvPayment.ExpandAll();
    }

    private string DeletePayment()
    {
      try
      {
        int paymentId = 0;
        if (int.TryParse(hfPaymentId.Value, out paymentId))
        {
          new PaymentService().Delete(paymentId);
        }

        return string.Empty;
      }
      catch (Exception ex)
      {
        WriteLog("Payment.aspx::DeletePayment", ex);
        return "Có lỗi trong quá trình lưu dữ liệu";
      }
    }

    private string DoPayment(int customerId)
    {
      try
      {
        int paymentId;
        if (int.TryParse(hfPaymentId.Value, out paymentId))
        {
          //Update
          new CustomerService().DoPayment(paymentId, customerId, Convert.ToInt32(txtMoney.Text), dePayingDate.Value as DateTime?, mmNote.Text, DateTime.Now, CurrentUser.Username);
        }
        else
        {
          //Insert
          new CustomerService().DoPayment(null, customerId, Convert.ToInt32(txtMoney.Text), dePayingDate.Value as DateTime?, mmNote.Text, DateTime.Now, CurrentUser.Username);
        }

        return string.Empty;
      }
      catch (Exception ex)
      {
        WriteLog("Payment.aspx::DoPayment", ex);
        return "Có lỗi trong quá trình lưu dữ liệu";
      }
    }
    #endregion
  }
}
